#include "MainMenuState.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "HighScoresState.h"
#include "GameOverState.h"
#include "GameplayState.h"
#include "OptionsState.h"
#include "TutorialState.h"
#include "SaveState.h"
#include "PauseState.h"
#include "IntroState.h"
#include "../DeadlyShadowsLastHope/Game.h"
#include "CreditState.h"
#include "LoadState.h"
#include "LevelState.h"

CMainMenuState* CMainMenuState::GetInstance(void)
{
	static CMainMenuState s_Instance;
	return &s_Instance;
}

CMainMenuState::CMainMenuState(void)
{
	m_nBackGroundImgID		= -1;
	m_nMenuCursor			= -1;
	m_nBackGroundSfxID		= -1;
	m_nBackGroundMusicID	= -1;
	m_nCursorMove			= 1;
	m_nOptionsLogoOff		= -1;
	m_nOptionsLogoOn		= -1;
	m_nHighScoreLogoOff		= -1;
	m_nHighScoreLogoOn		= -1;
	m_nLoadLogoOff			= -1;
	m_nLoadLogoOn			= -1;
	m_nHowToPlayOff			= -1;
	m_nHowToPlayOn			= -1;
	m_nPlay					= -1;
	m_nButton				= -1;
	m_nExit					= -1;
	m_nArrows				= -1;
}

CMainMenuState::~CMainMenuState(void) { }

void CMainMenuState::Enter(void)
{
//setup the state
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();

//IMAGES
	this->m_nBackGroundImgID = m_pTM->LoadTexture(L"../resources/graphics/main_menu_bg01.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nOptionsLogoOff = m_pTM->LoadTexture(L"../resources/graphics/options_menu_iconOFF.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nOptionsLogoOn = m_pTM->LoadTexture(L"../resources/graphics/options_menu_iconON.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nLoadLogoOn = m_pTM->LoadTexture(L"../resources/graphics/load_menu_icon_ON.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nLoadLogoOff = m_pTM->LoadTexture(L"../resources/graphics/load_menu_icon_OFF.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nHowToPlayOff = m_pTM->LoadTexture(L"../resources/graphics/how_to_play_menu_icon_OFF.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nHowToPlayOn = m_pTM->LoadTexture(L"../resources/graphics/how_to_play_menu_icon_ON.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nHighScoreLogoOn = m_pTM->LoadTexture(L"../resources/graphics/high_score_menu_icon_ON.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nHighScoreLogoOff = m_pTM->LoadTexture(L"../resources/graphics/high_score_menu_icon_OFF.png"/*, D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nButton = m_pTM->LoadTexture(L"../resources/graphics/ButtonOFF.png"/*D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nPlay = m_pTM->LoadTexture(L"../resources/graphics/PlayON.png"/*D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nExit = m_pTM->LoadTexture(L"../resources/graphics/ExitON.png"/*D3DCOLOR_XRGB(0, 0, 0)*/);
	this->m_nArrows = m_pTM->LoadTexture(L"../resources/graphics/arrows.png", D3DCOLOR_XRGB(0, 0, 0));


	//this->m_nMenuCursor = m_pTM->LoadTexture(_T("resource/graphics/CJM_SGD_MenuCursor.png"), D3DCOLOR_XRGB(0,0,0));
//SOUND
	//this->m_nBackGroundSfxID = m_pXA->SFXLoadSound(_T("resource/sounds/CJM_SFXBackGround.xwm"));
	//this->m_nBackGroundMusicID = m_pXA->MusicLoadSong(_T("resource/sounds/CJM_Music.xwm"));
	
	//if (!m_pXA->MusicIsSongPlaying(m_nBackGroundMusicID))
	//{
	//	m_pXA->MusicStopSong( m_nBackGroundMusicID );
	//	m_pXA->MusicPlaySong(m_nBackGroundMusicID, true);
	//}
}

void CMainMenuState::Exit(void)
{
//IMAGES
	if (this->m_nArrows != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nArrows);
		this->m_nArrows = -1;
	}
	if (this->m_nExit != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nExit);
		this->m_nExit = -1;
	}
	if (this->m_nPlay != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nPlay);
		this->m_nPlay = -1;
	}
	if (this->m_nButton != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nButton);
		this->m_nButton = -1;
	}
	if (this->m_nMenuCursor != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nMenuCursor);
		this->m_nMenuCursor = -1;
	}
	if (this->m_nBackGroundImgID != -1)
	{
		this->m_pTM->UnloadTexture( this->m_nBackGroundImgID );
		this->m_nBackGroundImgID = -1;
	}
	if (this->m_nOptionsLogoOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nOptionsLogoOff);
		this->m_nOptionsLogoOff = -1;
	}
	if (this->m_nOptionsLogoOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nOptionsLogoOn);
		this->m_nOptionsLogoOn = -1;
	}
	if (this->m_nLoadLogoOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLoadLogoOn);
		this->m_nLoadLogoOn = -1;
	}
	if (this->m_nLoadLogoOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLoadLogoOff);
		this->m_nLoadLogoOff = -1;
	}
	if (this->m_nHowToPlayOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nHowToPlayOn);
		this->m_nHowToPlayOn = -1;
	}
	if (this->m_nHowToPlayOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nHowToPlayOff);
		this->m_nHowToPlayOff = -1;
	}
	if (this->m_nHighScoreLogoOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nHighScoreLogoOn);
		this->m_nHighScoreLogoOn = -1;
	}
	if (this->m_nHighScoreLogoOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nHighScoreLogoOff);
		this->m_nHighScoreLogoOff = -1;
	}

//SOUND
	if( this->m_nBackGroundMusicID != -1 )
	{
		this->m_pXA->MusicStopSong( this->m_nBackGroundMusicID );
		this->m_pXA->MusicUnloadSong( this->m_nBackGroundMusicID );
		this->m_nBackGroundMusicID = -1;
	}
	if (this->m_nBackGroundSfxID != -1)
	{
		this->m_pXA->SFXStopSound(this->m_nBackGroundSfxID);
		this->m_pXA->SFXUnloadSound(this->m_nBackGroundSfxID);
		this->m_nBackGroundSfxID = -1;
	}
}

bool CMainMenuState::Input(void)
{
	if (m_pDI->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->RemoveState(this);
		CGame::GetInstance()->RemoveState(CLevelState::GetInstance());
		return false;
	}
	//if (m_pDI->KeyPressed(DIK_LEFT))
	//{
	//	if (m_nCursorMove == 1)
	//	{
	//		m_nCursorMove = 7;
	//	}
	//	m_nCursorMove--;
	//}
	//if (m_pDI->KeyPressed(DIK_RIGHT))
	//{
	//	if (m_nCursorMove == 6)
	//	{
	//		m_nCursorMove = 0;
	//	}
	//	m_nCursorMove++;
	//}
	if (m_nCursorMove== 1)
	{
		if (m_pDI->KeyPressed(DIK_UP))
		{
			m_nCursorMove = 2;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			m_nCursorMove = 5;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			m_nCursorMove = 4;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(COptionsState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if (m_nCursorMove == 2)
	{
		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			m_nCursorMove = 1;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			m_nCursorMove = 3;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(CLoadState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if (m_nCursorMove == 3)
	{
		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			m_nCursorMove = 4;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_LEFT))
		{
			m_nCursorMove = 2;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(CTutorialState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if (m_nCursorMove == 4)
	{	
		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			m_nCursorMove = 6;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_UP))
		{
			m_nCursorMove = 3;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_LEFT))
		{
			m_nCursorMove = 1;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(CHighScoresState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if (m_nCursorMove == 5)	//Level 0
	{
		if (m_pDI->KeyPressed(DIK_UP))
		{
			m_nCursorMove = 1;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			m_nCursorMove = 6;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(CLevelState::GetInstance());
			CGame::GetInstance()->AddState(CGameplayState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if(m_nCursorMove == 6)
	{
		if (m_pDI->KeyPressed(DIK_UP))
		{
			m_nCursorMove = 4;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_LEFT))
		{
			m_nCursorMove = 5;
			return true;
		}
		if (m_pDI->KeyPressed(DIK_RETURN))
		{
		CGame::GetInstance()->RemoveState(this);
		CGame::GetInstance()->RemoveState(CLevelState::GetInstance());
		return false;
		}
	}

//-----------------------------------------------------------------------
	return true;
}

void CMainMenuState::Update(float fElapsedTime)
{
	this->m_pXA->Update();
}

void CMainMenuState::Render(void)
{
	this->m_pTM->Draw(m_nBackGroundImgID, 0, 0, 0.8f, 0.6f, nullptr, 0, 0, 0);
	this->m_pD3D->GetSprite()->Flush();

	this->m_pTM->Draw(m_nArrows, 600, 400, 0.5f, 0.5f,nullptr, 0,0,0);


	if (m_nCursorMove == 1)
	this->m_pTM->Draw(m_nOptionsLogoOn, 425, 250, 0.6f, 0.6f,nullptr, 0,0,0);
	else 
	this->m_pTM->Draw(m_nOptionsLogoOff, 425, 250, 0.6f, 0.6f,nullptr, 0,0,0);

	if (m_nCursorMove == 2)
	this->m_pTM->Draw(m_nLoadLogoOn, 400, 50, 0.7f, 0.7f,nullptr, 0,0,0);
	else
	this->m_pTM->Draw(m_nLoadLogoOff, 400, 50, 0.7f, 0.7f,nullptr, 0,0,0);

	if (m_nCursorMove == 3)
	this->m_pTM->Draw(m_nHowToPlayOn, 600, 0, 0.7f, 0.7f,nullptr, 0,0,0);
	else
	this->m_pTM->Draw(m_nHowToPlayOff, 600, 0, 0.7f, 0.7f,nullptr, 0,0,0);

	if (m_nCursorMove == 4)
	this->m_pTM->Draw(m_nHighScoreLogoOff, 600, 200, 0.7f, 0.7f,nullptr, 0,0,0);	//named backwards
	else
	this->m_pTM->Draw(m_nHighScoreLogoOn, 600, 200, 0.7f, 0.7f,nullptr, 0,0,0);		//named backwards

//BUTTONs-------------------------------------------------------------------
	if (m_nCursorMove == 5)
	this->m_pTM->Draw(m_nPlay, 500, 500, 0.8f, 0.8f,nullptr, 0,0,0);
	else
	this->m_pTM->Draw(m_nButton, 500, 500, 0.8f, 0.8f,nullptr, 0,0,0);	//off

	if (m_nCursorMove == 6)
	this->m_pTM->Draw(m_nExit, 650, 500, 0.8f, 0.8f,nullptr, 0,0,0);
	else
	this->m_pTM->Draw(m_nButton, 650, 500, 0.8f, 0.8f,nullptr, 0,0,0);	//off


}